﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
using ZMotion;

namespace CustomModule
{
    [XmlInclude(typeof(ConveyerSetting))]
    public class ConveyerSetting
    {
        [XmlIgnore]
        string m_strName;
        public ConveyerSetting()
        {
            m_strName = "";
        }

        public static ConveyerSetting Load(string strName)
        {
            ConveyerSetting pDoc;
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(ConveyerSetting));
            FileStream fsReader = null;
            try
            {
                fsReader = File.OpenRead(Application.StartupPath + @"/MuduleParameter/ConveyerSetting" + "_" + strName + ".xml");
                pDoc = (ConveyerSetting)xmlSerializer.Deserialize(fsReader);
                pDoc.m_strName = strName;
                fsReader.Close();
            }
            catch (IOException)
            {
                if (fsReader != null)
                {
                    fsReader.Close();
                }
                pDoc = new ConveyerSetting();
                pDoc.m_strName = strName;
            }
            catch (Exception eMy)
            {
                if (fsReader != null)
                {
                    fsReader.Close();
                }
                pDoc = new ConveyerSetting();
                pDoc.m_strName = strName;
            }
            return pDoc;
        }
        public bool Save()
        {
            if (!Directory.Exists(System.Windows.Forms.Application.StartupPath + @"/MuduleParameter/"))
            {
                Directory.CreateDirectory(Application.StartupPath + @"/MuduleParameter/");
            }
            FileStream fsWriter = new FileStream(Application.StartupPath + @"/MuduleParameter/ConveyerSetting" + "_" + m_strName + ".xml", FileMode.Create, FileAccess.Write, FileShare.Read);
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(ConveyerSetting));
            xmlSerializer.Serialize(fsWriter, this);
            fsWriter.Flush(true);
            fsWriter.Close();

           

            return true;
        }
    }
}
